package com.chezai.client.widget;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Timer;
import java.util.TimerTask;


/**
 * TimerManager
 * Created by likunyang on 2017/11/3.
 */

public class TimerManager {
    private static TimerManager newInstance;
    private MyTimerTask timerTask;
    private Timer timer;
    private String TAG = this.getClass().getSimpleName();
    private boolean isStart;
    private OnTimerManagerListener mTimerListener;

    private TimerManager() {
    }

    public static TimerManager getInstance() {
        if (newInstance == null) {
            synchronized (TimerManager.class) {
                if (newInstance == null) {
                    newInstance = new TimerManager();
                }
            }
        }
        return newInstance;
    }

    public boolean isStart() {
        return isStart;
    }

    public void setOnTimerListener(TimerTextView listener) {
        this.mTimerListener = listener;
    }

    /**
     * 开始计时
     */
    public synchronized void start() {
        if (isStart) return;
        EventBus.getDefault().register(this);
        isStart = true;
        startTimer();
        if (mTimerListener != null) {
            mTimerListener.onTimerStart(this);
        }
    }

    /**
     * 停止计时
     */
    public synchronized void stop() {
        isStart = false;
        EventBus.getDefault().unregister(this);
        stopTimer();
        if (mTimerListener != null) {
            mTimerListener.onTimerStop(this);
        }
    }

    private void startTimer() {
        if (timer == null) {
            timer = new Timer(true);
        }
        if (timerTask == null) {
            timerTask = new MyTimerTask(this);
        }
        if (timerTask != null && timer != null) {
            timer.schedule(timerTask, 1000, 1000);
        }
    }


    private void stopTimer() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (timerTask != null) {
            timerTask.cancel();
            timerTask = null;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventTimerOneSecond(TimerManager timerManager) {
        if (mTimerListener != null) {
            mTimerListener.onTick(this);
        }
    }

    public void unRegisterTimerListener() {
        if (mTimerListener != null && !isStart) {
            mTimerListener = null;
        }
    }


    public interface OnTimerManagerListener {
        /**
         * 计时开始
         */
        void onTimerStart(TimerManager timerManager);

        void onTick(TimerManager timerManager);

        /**
         * 计时停止
         */
        void onTimerStop(TimerManager timerManager);
    }

    class MyTimerTask extends TimerTask {
        private TimerManager mTimerManager;

        public MyTimerTask(TimerManager timerManager) {
            this.mTimerManager = timerManager;
        }

        @Override
        public void run() {
            EventBus.getDefault().post(mTimerManager);
        }
    }

}
